package com.xtsoft.platform.base.system.controller;

import com.fasterxml.jackson.databind.JsonMappingException;

import com.xtsoft.core.controller.GenericController;
import com.xtsoft.core.repository.QueryFilter;
import com.xtsoft.core.repository.QueryResult;
import com.xtsoft.core.utils.HeaderUtil;
import com.xtsoft.core.utils.ResultMessage;
import com.xtsoft.core.utils.StringUtils;
import com.xtsoft.platform.base.system.domain.SysTrade;
import com.xtsoft.platform.base.system.service.SysTradeService;
import com.xtsoft.platform.globe.enums.YesNo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParseException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;
/**
 * @ClassName: SysTrade
 * @Author author
 * @Vesion 1.0
 * Create Time:
 * <p><b>Class Description:</b>[TODO]<p>
 * REST controller for managing sysTrade.
 */

@Tag(name = "系统行业--处理接口")
@RestController
public class SysTradeController extends GenericController{
	
	private final Logger log = LoggerFactory.getLogger(SysTradeController.class);
	
	@Autowired	
	private SysTradeService sysTradeService;
	
    /**
     * 功能：新增一个实体，POST请求  
     * 
     * 路径：  /sysTrade/add
     *
     * @param sysTrade 要新增的实体
     * @return ResponseEntity, json格式,http status 201 表示成功 , html body中包含 sysTrade
     * 若sysTrade存在主键值, 则http status 400 (Bad Request)
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
     */
	
	@Parameter(name = "sysTrade", description = "实体sysTrade", required = false)
	@Operation(summary  = "新增一个实体")
    @PostMapping("/sysTrade/add")
    public ResponseEntity<SysTrade> add(@RequestBody SysTrade sysTrade) throws URISyntaxException {
        log.debug("REST request 保存 : SysTrade", sysTrade);
        if (!StringUtils.isEmpty(sysTrade.getSysTradeId())) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("sysTrade", "idexists", "A new sysTrade cannot already have an ID")).body(null);
        }
        sysTrade.setState(YesNo.Yes.getIntValue());
        sysTrade.setPreSysTradeId("00");
        SysTrade result = sysTradeService.save(sysTrade);
        return ResponseEntity.created(new URI("/sysTrade/get/" + result.getSysTradeId()))
            .headers(HeaderUtil.createEntityCreationAlert("sysTrade", result.getSysTradeId().toString()))
            .body(result);
    }	

    /**
     * 功能：更新一个实体，PUT请求  
     * 
     * 路径：  /sysTrade/update
     *
     * @param sysTrade 要更新的 sysTrade
     * @return ResponseEntity, json格式, http status 200 表示成功 , html body中包含 sysTrade,
     * 如果sysTrade无效，则http status 404  (Bad Request)
     * 如果sysTrade不能更新，则http status 500 (Internal Server Error)
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
     */
	
	@Parameter(name = "sysTrade", description = "实体sysTrade", required = false)
	@Operation(summary  = "更新一个实体")
    @PostMapping("/sysTrade/update")
    public ResponseEntity<SysTrade> update(@RequestBody SysTrade sysTrade) throws URISyntaxException {
        log.debug("REST request 更新： SysTrade", sysTrade);
        if (StringUtils.isEmpty(sysTrade.getSysTradeId())) {
            return add(sysTrade);
        }
        sysTrade.setState(YesNo.Yes.getIntValue());
        sysTrade.setPreSysTradeId("00");
        SysTrade result = sysTradeService.save(sysTrade);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("sysTrade", sysTrade.getSysTradeId().toString()))
            .body(result);
    }  

    /**
     * 功能：获取分页的实体集合，GET请求  
     * 
     * 路径：  /sysTrade/getAll
     *

     * @return the ResponseEntity, json格式，  http status 200 表示成功  , html body中包含 SysTrade的 list集合
     * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
     */
	
	@Operation(summary  = "获取分页的实体集合")
    @GetMapping("/sysTrade/getAll")
    public ResponseEntity<QueryResult<SysTrade>> getAll()
        throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
        log.debug("REST request 获取页：SysTrade");
        QueryFilter filter=new QueryFilter(SysTrade.class);
        filter.addFilterFromRequest(request);
        QueryResult<SysTrade> result= sysTradeService.anyQuery(filter);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 功能：根据主键值获取一个实体对象，GET请求
     *
     * 路径： /sysTrade/get/:id
     *
     * @param id 实体主键值
     * @return  ResponseEntity, json格式, http status 200 表示成功 , html body中包含 SysTrade, http status 404 表示未找到
     */
	
	@Parameter(name = "id", description = "主键id", required = false)
	@Operation(summary  = "根据主键值获取一个实体对象")
    @GetMapping("/sysTrade/get/{id}")
 
    public ResponseEntity<SysTrade> get(@PathVariable String id) {
        log.debug("REST request 获取： SysTrade", id);
        SysTrade sysTrade = sysTradeService.get(id);
        return Optional.ofNullable(sysTrade)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    } 

    /**
     * 功能：根据主键值删除一个实体对象，DELETE请求
     *
     * 路径： /sysTrade/delete/:id
     *
     * @param  id 实体主键值
     * @return ResponseEntity,json格式, http status 200 (OK)
     */
	
	@Parameter(name = "id", description = "主键id", required = false)
	@Operation(summary  = "根据主键值删除一个实体对象")
    @GetMapping("/sysTrade/delete/{id}")  
    public ResponseEntity<ResultMessage> delete(@PathVariable String id) {
        log.debug("REST request 删除： SysTrade", id);
        SysTrade sysTrade=sysTradeService.get(id);
        if(null!=sysTrade) {
        	if(sysTrade.getSysOrganizes().size()>0) {
        		return new ResponseEntity<ResultMessage>(new ResultMessage(false,"该行业已经被企业引用，不能删除！"),HttpStatus.OK);
        	}
        	sysTradeService.remove(sysTrade);
        }
        return new ResponseEntity<ResultMessage>(new ResultMessage(true),HttpStatus.OK);
    }
    

	/**
	 * @Description 变更行业状态
	 * @Param id
	 * @return org.springframework.http.ResponseEntity<com.xtsoft.core.utils.ResultMessage>
	 * @Author defencez
	 * @Date 2024-04-21 12:38
	*/
	@Parameter(name = "id", description = "主键id", required = false)
	@Operation(summary  = "变更行业状态")
    @GetMapping("/sysTrade/isVisiable/{id}")
    public ResponseEntity<ResultMessage> isVisiable(@PathVariable String id){
    	SysTrade sysTrade=sysTradeService.get(id);
    	if(null!=sysTrade) {
    		if(sysTrade.getState().equals(YesNo.No.getIntValue())) {
    			sysTrade.setState(YesNo.Yes.getIntValue());
    		}else {
    			sysTrade.setState(YesNo.No.getIntValue());
    		}
    		sysTradeService.save(sysTrade);
    	}
    	return new ResponseEntity<ResultMessage>(new ResultMessage(true),HttpStatus.OK);
    }
    
    /**
     * 功能: 获取行业下拉菜单  
     * 	只允许选择启用的
     * @return
     * @return ResponseEntity<List<Object>>
     * @date 2018年8月7日 下午2:47:14
     * @auth kezhuan
     */
	@Operation(summary  = "获取行业下拉菜单")
    @GetMapping("/sysTrade/getCombox")
    public ResponseEntity<List<SysTrade>> getCombox(){
    	return new ResponseEntity<>(sysTradeService.getCombox(),HttpStatus.OK);
    }
    

    
    
}
